package com.tubeonfire.controller.admin;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tubeonfire.blobstore.BlobKeyProcess;
import com.tubeonfire.entity.Advertising;
import com.tubeonfire.model.admin.AdvertisingModel;

@SuppressWarnings("serial")
public class EditAdvertisingServlet extends HttpServlet {

	private static final Logger log = Logger
			.getLogger(EditAdvertisingServlet.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		try {
			HttpSession session = req.getSession();
			String id = req.getParameter("id");
			Advertising obj = AdvertisingModel.byId(id, false);
			if (obj != null) {
				session.setAttribute("action", "edit");
				session.setAttribute("obj", obj);
				req.getRequestDispatcher("/admin/form_advertising.jsp")
						.forward(req, resp);
			} else {
				session.setAttribute("error",
						"Advertising's not exists or has been deleted !");
				resp.sendRedirect("/admin/advertising/manage?place=1");
			}
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			resp.sendError(4004,
					"We are sorry for the inconvenience ! Please try again later !");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String id = req.getParameter("id");
			Advertising obj = AdvertisingModel.byId(id, false);
			HttpSession session = req.getSession();
			if (obj != null) {
				int type = 0;
				int place = 0;
				int position = 0;
				String strType = req.getParameter("advertisingType");
				String strPlace = req.getParameter("place");
				String strPosition = req.getParameter("position");
				try {
					type = Integer.parseInt(strType);
				} catch (Exception e) {
					type = 0;
				}
				try {
					place = Integer.parseInt(strPlace);
				} catch (Exception e) {
					place = 0;
				}
				try {
					position = Integer.parseInt(strPosition);
				} catch (Exception e) {
					position = 0;
				}
				if (type == 1 && place != 0 && position != 0) {
					String content = req.getParameter("content");
					obj.setType(type);
					obj.setPlace(place);
					obj.setPosition(position);
					obj.setContent(content);
					AdvertisingModel.insert(obj);
					session.setAttribute("success", "Action success !");
					resp.sendRedirect("/admin/advertising/manage?place="
							+ place);
				} else if (type == 2 && place != 0 && position != 0) {
					String title = (String) req.getParameter("title");
					String link = (String) req.getParameter("link");
					String imgKey = BlobKeyProcess.getBlobKey(req, "image");
					obj.setType(type);
					obj.setPlace(place);
					obj.setPosition(position);
					obj.setTitle(title);
					obj.setLink(link);
					obj.setContent(imgKey);
					AdvertisingModel.insert(obj);
					session.setAttribute("success", "Action success !");
					resp.sendRedirect("/admin/advertising/manage?place="
							+ place);
				} else {
					resp.sendError(4004, "Invalid parameter !");
				}
			} else {
				session.setAttribute("error",
						"Advertising's not exists or has been deleted !");
				resp.sendRedirect("/admin/advertising/manage?place=1");
			}
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			resp.sendError(4004,
					"We are sorry for the inconvenience ! Please try again later !");
		}
	}
}
